<?php

class Skjb_Office_Spreadsheet_Sheet
{
    public $rows = array();
    
    public function __construct($rows = null)
    {
        if ($rows !== null) {
            $this->rows = $rows;
        }
    }
    
    public function addRow($inRow = null)
    {
        if ($inRow === null) {
            $row = new Skjb_Office_Spreadsheet_Sheet_Row();
        } elseif (is_array($inRow)) {
            $row = new Skjb_Office_Spreadsheet_Sheet_Row($inRow);
        } elseif ($inRow instanceof Skjb_Office_Spreadsheet_Sheet_Row) {
            $row = $inRow;
        }
        
        $this->rows[] = $row;
        
        return $row;
    }
    
    public function addRows(array $inRows)
    {
        foreach ($inRows as $inRow) {
            $this->addRow($inRow);
        }
    }
    
    public function populateRows($fields, $objects)
    {
        foreach ($objects as $object) {
            $row = $this->addRow();
            foreach ($fields as $field) {
                if (strpos($field, '(') === false) {
                    $row->addCell($object->$field);
                } else {
                    // Do function call
                    $function = substr($field, 0, -2);
                    $row->addCell($object->$function());
                }
            }
        }
    }
}